<?php

class Application_Model_Dao_Subject extends Zend_Db_Table_Abstract
{

	protected $_name = 'subjects';
	protected $_primary = 'id';

	public function init(){
		 
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @throws Exception
	 */
	public function getAll(){
		$result = $this->fetchAll();
		if( !$result )
		throw new Exception("Wystąpił błąd przy pobieraniu danych z bazy. Proszę spróbować jeszcze raz. Jeśli problem będzie się powtarzać, prosimy skontaktować się z administratorem.");
		return $result;
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param $id
	 */
public function getRowSubject($id){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('subjects')->where('subjects.id = \''.$id.'\'');
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}
	
	/**
	 * 
	 * Enter description here ...
	 */
	public function getAllSubject(){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('subjects');
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param $id
	 */
	public function getSubjectByID($id) {
		if( sizeof($id) == 1 ) {
			$id = (int)$id;
			$row = $this->fetchRow('id = '.$id);
			if( !$row )
				throw new Exception("Przedmiot o ID $id nie istnieje.");
			return $row->toArray();
		} else if( sizeof($id) > 1 ) {
			$rows = array(array()); $i=0;
			$row;
			for( $i=0; $i<sizeof($id); $i++ ) {
				$row = $this->fetchRow('id = '.$id[$i]);
					$rows[$i] = $row;
			}
			
			/*
			$string = "";
			for( $i=0; $i<sizeof($id); $i++ ) {
				$string .= 'id = '.$id[$i].' ';
				if( sizeof($id)-1 != $i )
					$string .= ' OR ';				
			}
			$rows = $this->fetchRow($string);
			*/
			return $rows; //->toArray();
		}
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param $designation
	 */
	public function getSubjectByDesignation(string $designation){
		$row = $this->fetchRow('designation = '.$designation);
		if (!$row)
		throw new Exception("Przedmiot o nazwie $designation nie istnieje.");
		return $row->toArray();
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param array $subject
	 */
	public function saveSubject($subject) {
		$data = array(
			'designation' => $subject->getDesignation(),
			'short' => $subject->getShort(),
		);
		$result = $this->insert($data);
		return $result;
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param array $subject
	 */
	public function updateSubject($subject) {
		$data = array(
			'designation' => $subject->getDesignation(),
			'short' => $subject->getShort(),
		);
		$result = $this->update($data, 'id = '. (int)$subject->getId());
		return $result;
	}
	
	/**
	 * 
	 * Usuwa przedmiot/-y po jego/ich ID
	 * @param $subjectID - id przedmiotu
	 */
	public function deleteSubject($subjectID) {
		if( 1 == sizeof($subjectID) ) {
			try {
				$result = $this->delete('id =' . (int)$subjectID->getId());
			} catch (Exception $exception) {
				Zend_Registry::get('logger')->log('Usuwanie przedmiotu po jego id nie powiodło się. Exception: '.$exception, Zend_Log::NOTICE);
			}
		} else {
			try {
			 $result = $this->delete($_subjects, array('id = ?' => $subjectID));
			} catch (Exception $exception) {
				Zend_Registry::get('logger')->log('Usuwanie przedmiotu po jego id nie powiodło się. Exception: '.$exception, Zend_Log::NOTICE);
			}
		}
		return $result;
	}
	
	/**
	 * 
	 * Enter description here ...
	 */
	public function getToSelect(){
		$array = $this->getAll()->toArray();
		$subjects[''] = "wybierz";
		foreach ($array as $option) {
			$subjects[ $option['id'] ] = $option['designation' ] ;
		}
		return $subjects;
	}

}
 